package com.atguigu.mapreduce.demo.combiner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/13 20:07
 * @description:
 */
public class WordCountDriver {

    static String srcPath = "D:\\testarea\\bigdatatest\\hadoop\\mapreduce\\11_input\\inputword\\hello.txt";
    static String dstPath = "D:\\testarea\\bigdatatest\\hadoop\\mapreduce\\11_input\\inputword\\out";

    /**
     * D:\testarea\bigdatatest\hadoop\mapreduce\11_input\inputword\hello.txt
     * D:\testarea\bigdatatest\hadoop\mapreduce\11_input\inputword\helloCount.txt
     *
     *
     */
    public static void main(String[] args) {

        try {
            // job
            Configuration conf = new Configuration();
            Job job = Job.getInstance(conf);

            // jar path
            job.setJarByClass(WordCountDriver.class);

            // mapper  reducer  关联
            job.setMapperClass(WordCountMapper.class);
            job.setReducerClass(WordCountReduce.class);

            // map out k v
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);

            // reducer out k v 有的可能不存在
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);

            //切片设置
            job.setInputFormatClass(CombineTextInputFormat.class);
            CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);//4M

            //combiner setting
//            job.setCombinerClass(WordCountCombiner.class);
            //combiner 与 reduce十分接近, 直接使用reduce代码充当Combiner代码即可
            job.setCombinerClass(WordCountReduce.class);


            // 分析哪个文件? 结果放在哪里? 指不指定文件看情况, 都行, 别瞎搞就可以
            // 从主方法中动态传入路径
//            FileInputFormat.setInputPaths(job, new Path(args[0]));
//            FileOutputFormat.setOutputPath(job, new Path(args[1]));
            FileInputFormat.setInputPaths(job, new Path(srcPath));
            FileOutputFormat.setOutputPath(job, new Path(dstPath));

            // 提交job
            boolean resFlag = job.waitForCompletion(true);
            System.exit(resFlag ? 0 : 1);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

